package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.CartBo;
import com.cskaoyan.bean.wx.CartCheckedOutVO;
import com.cskaoyan.bean.wxbean.CartCheckedBo;
import com.cskaoyan.bean.wxbean.CartIndexVO;
import com.cskaoyan.bean.wxbean.CartTotal;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/10/6 13:57
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    ConfigMapper configMapper;

    @Override
    public CartIndexVO getCartIndex(Integer i) {
        CartIndexVO cartIndexVO = new CartIndexVO();
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andUserIdEqualTo(i);
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        ArrayList<Cart> arrayList = new ArrayList();
        for (Cart cart : carts) {
            if(cart.getDeleted()){
                arrayList.add(cart);
            }
        }
        cartIndexVO.setCartList(arrayList);

        Integer goodsCount = 0;
        Integer checkedGoodsCount = 0;
        Double goodsAmount = 0.0;
        Double checkedGoodsAmount = 0.0;

        for (Cart cart : arrayList) {
            goodsCount = goodsCount + cart.getNumber();
            goodsAmount = goodsAmount + cart.getPrice().doubleValue();
            if (cart.getChecked()) {
                checkedGoodsCount = checkedGoodsCount + cart.getNumber();
                checkedGoodsAmount = checkedGoodsAmount + cart.getNumber() * cart.getPrice().doubleValue();
            }
        }
        CartTotal cartTotal = new CartTotal(goodsCount, checkedGoodsCount, goodsAmount, checkedGoodsAmount);
        cartIndexVO.setCartTotal(cartTotal);
        return cartIndexVO;
    }

    @Override
    public void cartUpdate(Integer id, Integer number) {
        Date date = new Date();
        cartMapper.cartUpdate(id, number, date);
    }

    @Override
    public CartIndexVO cartDelete(Integer productId) {
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andProductIdEqualTo(productId);
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        Integer id = carts.get(0).getUserId();
        cartMapper.deleteByExample(cartExample);
        return getCartIndex(id);
    }

    @Override
    public CartIndexVO cartChecked(CartCheckedBo cartCheckedBo, Integer userId) {
        List<Integer> productIds = cartCheckedBo.getProductIds();
        Date date = new Date();
        for (Integer productId : productIds) {
            cartMapper.cartChecked(productId, cartCheckedBo.getIsChecked(), date, userId);
        }
        return getCartIndex(userId);
    }

    @Override
    public CartCheckedOutVO CartCheckOut(Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId, Integer userId) {
        CartIndexVO cartIndex = getCartIndex(userId);
        Double goodsTotalPrice = cartIndex.getCartTotal().getCheckedGoodsAmount();


        Integer availableCouponLength = couponUserMapper.countCouponNumber(userId,0);

        List<Cart> cartList = cartIndex.getCartList();
        ArrayList<Cart> checkedGoodsList = new ArrayList<>();
        if(cartId==0) {
            for (Cart cart : cartList) {
                if (cart.getChecked()) {
                    checkedGoodsList.add(cart);
                }
            }
        }else{
            CartExample cartExample = new CartExample();
            cartExample.createCriteria().andUserIdEqualTo(userId);
            List<Cart> carts = cartMapper.selectByExample(cartExample);
            Cart cart=carts.get(carts.size()-1);
            double v = cart.getPrice().doubleValue() * cart.getNumber();
            goodsTotalPrice=v;
            checkedGoodsList.add(cart);
        }



        Address checkedAddress = addressMapper.selectByPrimaryKey(addressId);

        List<Integer> integers = couponUserMapper.selectCouponIdByUserId(userId);
        Coupon coupon = null;
        if (!integers.isEmpty()) {
            coupon = couponMapper.selectByPrimaryKey(couponId);
        }
        Double couponPrice;
        if (coupon != null) {
            if(couponId==-1){
                coupon.setId(-1);
                couponPrice=0.0;
            }else {
                couponPrice = coupon.getDiscount().doubleValue();
            }
        }else {
            coupon=new Coupon();
            coupon.setId(-1);
            couponPrice=0.0;
        }
        Double actualPrice = 0.0;
        actualPrice=goodsTotalPrice;
        if(actualPrice<99){
            availableCouponLength=0;
            couponPrice=0.0;
            coupon.setId(0);
        }
        actualPrice = actualPrice - couponPrice;
        Double orderTotalPrice = 0.0;
        orderTotalPrice = actualPrice;
        Double freighPrice = 0.0;
        Double grouponPrice = 0.0;
        Config config = configMapper.selectByPrimaryKey(37);
        String min = config.getKeyValue();
        if(goodsTotalPrice<Integer.valueOf(min)){
            Config config1 = configMapper.selectByPrimaryKey(39);
            String keyValue = config1.getKeyValue();
            freighPrice= Double.valueOf(keyValue);
            actualPrice=actualPrice+Integer.valueOf(keyValue);
        }


        return new CartCheckedOutVO(actualPrice, addressId, availableCouponLength, checkedAddress, checkedGoodsList,
                coupon.getId(), couponPrice, freighPrice, goodsTotalPrice, grouponPrice, grouponRulesId, orderTotalPrice);
    }

    @Override
    public Integer getGoodsCount(Integer id) {
        Integer i = cartMapper.getGoodsCount(id);
        return i;
    }

    @Override
    public long cartAdd(CartBo cartBo, Integer id) {
        Date date = new Date();
        Cart cartInfo = cartMapper.queryCartInfo(cartBo.getGoodsId(), cartBo.getProductId());
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andUserIdEqualTo(id).andGoodsIdEqualTo(cartBo.getGoodsId()).andProductIdEqualTo(cartBo.getProductId());
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        if (!carts.isEmpty()) {
            for (Cart cart : carts) {
                cart.setUpdateTime(date);
                int i = cart.getNumber() + cartBo.getNumber();
                cart.setNumber(i);
                CartExample cartExample1 = new CartExample();
                cartExample1.createCriteria().andUserIdEqualTo(id).andGoodsIdEqualTo(cart.getGoodsId());
                cartMapper.updateByExampleSelective(cart,cartExample1);

            }
        } else {
            cartInfo.setUserId(id);
            cartInfo.setGoodsId(cartBo.getGoodsId());
            cartInfo.setGoodsSn(String.valueOf(cartBo.getGoodsId()));
            cartInfo.setProductId(cartBo.getProductId());
            cartInfo.setNumber(cartBo.getNumber());
            cartInfo.setUpdateTime(date);
            cartInfo.setChecked(true);
            cartInfo.setDeleted(true);
            cartInfo.setAddTime(new Date());
            Goods goods = goodsMapper.selectByPrimaryKey(cartBo.getGoodsId());
            cartInfo.setGoodsName(goods.getName());
            cartInfo.setPicUrl(goods.getPicUrl());
            cartMapper.insert(cartInfo);
        }
//        cartExample.createCriteria().andUserIdEqualTo(id).andDeletedEqualTo(false);
        CartExample cartExample1 = new CartExample();
        cartExample1.createCriteria().andUserIdEqualTo(id);
        long total = cartMapper.countByExample(cartExample1);
        return total;
    }

    @Override
    public Integer fastAdd(CartBo cartBo, Integer id) {
        Date date = new Date();
        Cart cartInfo = cartMapper.queryCartInfo(cartBo.getGoodsId(), cartBo.getProductId());
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andUserIdEqualTo(id).andGoodsIdEqualTo(cartBo.getGoodsId()).andProductIdEqualTo(cartBo.getProductId());
        cartInfo.setUserId(id);
        cartInfo.setGoodsId(cartBo.getGoodsId());
        cartInfo.setGoodsSn(String.valueOf(cartBo.getGoodsId()));
        cartInfo.setProductId(cartBo.getProductId());
        cartInfo.setNumber(cartBo.getNumber());
        cartInfo.setUpdateTime(date);
        cartInfo.setChecked(false);
        cartInfo.setDeleted(false);
        cartInfo.setAddTime(new Date());
        Goods goods = goodsMapper.selectByPrimaryKey(cartBo.getGoodsId());
        cartInfo.setGoodsName(goods.getName());
        cartInfo.setPicUrl(goods.getPicUrl());
        cartMapper.insert(cartInfo);

        Integer cartId = cartMapper.queryLastInsert();
        return cartId;
    }

}
